
# Set up the packages, options, and working directory path
pacman::p_load(tidyverse)
set.seed(42)
options(scipen=999)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
}

# Load function for making tex table rows
source(file.path(root, "code/tables_figures/prep_table_rows.R"))

# Bring in the expenditure tercile estimates
load(file.path(root, "tmp/synth_vapTerciles.RData"))

# Set up the matrices for storing output
out = matrix(nrow=6, ncol=6)

# Name the rows and columns of the output matrices
estimators = c("vs_small", "vs_mid", "vs_large", 
  "to_small", "to_mid", "to_large")
output_components = c("te estimate", "se", "num years", "treated counties", "num counties", "n")
colnames(out) = estimators
rownames(out) = output_components

# Store the estimated effects and standard errors
out[1:2,] = cbind(vap_t1_synth_dem$summary_table[,1],
  vap_t2_synth_dem$summary_table[,1],
  vap_t3_synth_dem$summary_table[,1],
  vap_t1_synth_tur$summary_table[,1],
  vap_t2_synth_tur$summary_table[,1],
  vap_t3_synth_tur$summary_table[,1])

# Count the number of years and units
c = 1
for(all_ests in list(vap_t1_synth_dem, vap_t2_synth_dem, 
      vap_t3_synth_dem, vap_t1_synth_tur, 
      vap_t2_synth_tur, vap_t3_synth_tur)){
    est_setup = attr(all_ests$ests[[1]], "setup")
    out[3,c] = est_setup$T0 + 1
    out[4,c] = nrow(est_setup$Y) - est_setup$N0
    out[5,c] = nrow(est_setup$Y)
    out[6,c] = nrow(est_setup$Y)*ncol(est_setup$Y)
    c = c + 1
}

# Put the estimates in a tex table
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{Election Administration Grants Did Not Noticeably Advantage Democrats or Increase Turnout in 2020, Voting Age Population Tercile.}",
"\\label{tab:vap_tercile}}",
"\\begin{tabular}{lccc|ccc}",
"\\toprule \\toprule",
" & \\multicolumn{3}{c}{Dem Vote Share (\\%)} & \\multicolumn{3}{c}{Turnout (\\%)} \\\\",
" & Small & Medium & Large & Small & Medium & Large \\\\",
" & Population & Population & Population & Population & Population & Population \\\\",
"\\midrule",
paste0("Grant Recipient in 2020", prep_table_rows(out[1,]), " \\\\"),
paste0(prep_table_rows(out[2,], se=TRUE), " \\\\[2mm]"),
paste0("Num Grant Recipients ", prep_table_rows(out[4,], wholenum=TRUE), " \\\\"),
paste0("Num Counties ", prep_table_rows(out[5,], wholenum=TRUE), " \\\\"),
paste0("Observations ", prep_table_rows(out[6,], wholenum=TRUE), " \\\\"),
"\\midrule",
"County FEs & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"Year FEs & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"County Weights & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"Year Weights & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{7}{p{1.1\\textwidth}}{\\footnotesize Standard errors estimated with 1,000 county block ",
"bootstrap samples reported in parentheses. Data is a balanced panel of counties in the 8 presidential ",
"elections from 1992 to 2020. Small, medium, and large population counties are defined by terciles of ",
"voting age population.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/vap_effects_table.tex"), sep="\n")

